package org.edu.modules.sxjy.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils
{
    private static SimpleDateFormat sf = null;

    public static String getCurrentDate()
    {
        Date d = new Date();
        sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(d);
    }

    public static String getDateToString(long time)
    {
        Date d = new Date(time);
        sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(d);
    }

    public static String timestampToDate(long time)
    {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long timeLong = Long.valueOf(time).longValue();
        String dateTime = simpleDateFormat.format(new Date(timeLong * 1000L));
        return dateTime;
    }

    public static long getStringToDate(String time)
    {
        sf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try
        {
            date = sf.parse(time);
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }
        return date.getTime();
    }

    public static String getTimeStamp()
    {
        String currentDate = getCurrentDate();
        sf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try
        {
            date = sf.parse(currentDate);
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }
        return String.valueOf(date.getTime());
    }

    public static long dateToTimestamp(String time)
    {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try
        {
            Date date = simpleDateFormat.parse(time);
            return date.getTime() / 1000L;
        }
        catch (ParseException e) {}
        return 0L;
    }

    public static String timeStamp2Date(String seconds, String format)
    {
        if ((seconds == null) || (seconds.isEmpty()) || (seconds.equals("null"))) {
            return "";
        }
        if ((format == null) || (format.isEmpty())) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds + "000").longValue()));
    }

    public static String date2TimeStamp(String date_str, String format)
    {
        try
        {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(date_str).getTime() / 1000L);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return "";
    }
}
